//
//  MerchantCardChangeViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/26.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "MerchantCardChangeViewController.h"
#import "WYIDScanViewController.h"
#import "ChangeCardParametersModel.h"
#import "PostWorkOrderSuccessViewController.h"
#import "ChangeCardStore.h"

@interface MerchantCardChangeViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    
    __weak IBOutlet UIScrollView *sView;
    
    __weak IBOutlet UITextField *shNameTf;
    __weak IBOutlet UITextField *shNoTf;
    
    __weak IBOutlet UITextField *cardNoTf;
    __weak IBOutlet UITextField *bankNameTf;
    
    __weak IBOutlet UITextField *khTf;
}

@property (nonatomic,strong) ChangeCardParametersModel *parametersModel;

@property (nonatomic,weak) UIButton *currentBtn;

@end

@implementation MerchantCardChangeViewController

- (ChangeCardParametersModel *)parametersModel{
    
    if (!_parametersModel) {
        
        _parametersModel = [[ChangeCardParametersModel alloc] init];
        _parametersModel.customId = [UserDefaultsManager sharedInstance].user_id;
    }
    
    return _parametersModel;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:YES];
    self.currentBtn.enabled = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];
}

- (void)setUI{
    
    [self setNavBarWithTitle:@"结算卡更换申请"];
    
    [sView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.equalTo(self.view);
        
    }];
}



- (IBAction)onBankCardFontBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    sender.enabled = NO;
    
    self.currentBtn = sender;
    
    WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDBank];
    
    [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
        
         NSString *str = [scanModel.bankNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
        self->cardNoTf.text = str;
        self->bankNameTf.text = scanModel.bankName;
        
        self.parametersModel.bankFont = [ImgManager imageToString:scanModel.bankImage];
        
        [sender setImage:scanModel.bankImage forState:UIControlStateNormal];
    }];
    
    [self.navigationController pushViewController:VC animated:YES];
}

- (IBAction)onBankCardBackBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    self.currentBtn = sender;
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        return [self showMBPError:@"未发现摄像头"];
        
    }
    
    [self takePhoto];
    
}

- (void)takePhoto{
    
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    [self presentViewController:picker animated:YES completion:nil];
    
}
#pragma mark - 照片选择完成
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
        
        self.parametersModel.bankBack = [ImgManager imageToString:image];
        
        [self.currentBtn setImage:image forState:UIControlStateNormal];
        
    }];
}

#pragma mark - 确认并提交
- (IBAction)onPostBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (shNameTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入商户姓名"];
    }
    
    self.parametersModel.merchantName = shNameTf.text;
    
    if (shNoTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入商户编号"];
    }
    
    self.parametersModel.merchantNo = shNoTf.text;
    
    if (self.parametersModel.bankFont.length <= 0) {
        
        return [self showMBPError:@"请上传结算卡正面照片"];
    }
    
    if (self.parametersModel.bankBack.length <= 0) {
        
        return [self showMBPError:@"请上传结算卡背面照片"];
    }
    
    if (cardNoTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入结算卡号"];
    }
    
    self.parametersModel.bankCardNumber = cardNoTf.text;
    
    if (bankNameTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入结算银行"];
    }
    
    self.parametersModel.bank = bankNameTf.text;
    
    if (khTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入开户银行"];
    }
    
    self.parametersModel.khzh = khTf.text;

    [self.view makeToastActivity:CSToastPositionCenter];
    
    ChangeCardStore *store = [[ChangeCardStore alloc] init];
    
    MJWeakSelf
    [store postChangeCardDataWithParameters:self.parametersModel Success:^{
        
        [weakSelf.view hideToastActivity];
        
        PostWorkOrderSuccessViewController *VC = [[PostWorkOrderSuccessViewController alloc] init];
        [weakSelf.navigationController pushViewController:VC animated:YES];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.view hideToastActivity];
        [weakSelf showMBPError:[HttpTool handleError:error]];
        
    }];
    
}

@end
